/*
 * KIMWONSORK@GMAIL.COM
 * COPYRIGHT CRAYONSOFT All rights reserved.
 * 2013오후 9:07:00
 * kws
 */

package com.crayon.account.fragments;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import com.crayon.account.dao.DAOAccount;
import com.crayon.account.data.ENMapKey;
import com.crayon.account.data.ItemGroup;
import com.crayon.account.fragments.ui.FragmentTopSum;
import com.crayon.account.impl.OnDateChangeListener;
import com.crayon.account.impl.OnDialogFragment;
import com.crayon.account.loader.LoaderSQLAccountBetween;
import com.crayon.sharemenu.R;
import com.crayon.util.GDB;
import com.crayon.util.UTILDate;
import com.crayon.util.UTILSC;

import net.fortuna.ical4j.model.Recur;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class FragmentChildRoot extends Fragment implements 
LoaderManager.LoaderCallbacks<HashMap<ENMapKey, Object>>
,OnDialogFragment
,OnDateChangeListener
{
	public static final String PARAM_DATE="paramDate";
	Date mDate =new Date();
	public static FragmentChildRoot onCrateInstance(long time){
		FragmentChildRoot f = new FragmentChildRoot();
		Bundle args =new Bundle();
		args.putLong(PARAM_DATE, time);
		f.setArguments(args);
		return f;
	}


    /**
     * 검색기간 시 일간/ 주간/ 원간을 구분해주기 위한 인터페이스..
     */
    public interface ONDataPeroidBewteenListener{
        public String ONDataPeriodBewteen(Date date,Class cls);

    }

    ONDataPeroidBewteenListener dataPeroidBewteenListener;

    public ONDataPeroidBewteenListener getDataPeroidBewteenListener() {
        return dataPeroidBewteenListener;
    }

    public void setDataPeroidBewteenListener(ONDataPeroidBewteenListener dataPeroidBewteenListener) {
        this.dataPeroidBewteenListener = dataPeroidBewteenListener;
    }

    HashMap<String, LinkedList<DAOAccount>> hashMap =new HashMap<String, LinkedList<DAOAccount>>();
	List<Map<String,ItemGroup>> groupDatas=new ArrayList<Map<String,ItemGroup>>();
	List<List<Map<String,DAOAccount>>> childData=new ArrayList<List<Map<String,DAOAccount>>>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		if(getArguments()!=null){
			mDate.setTime(getArguments().getLong(PARAM_DATE));
		}
	}

	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View rootView = inflater.inflate(R.layout.fragment_child_exlist,container, false);
		
		return rootView;
	}



	
	protected void onWriteCal(DAOAccount item){

		UTILSC.addOnPopWriteFragment(getActivity(), item, this);
	}

	@Override
	public void onDismissDialog(Fragment fragment) {
		// TODO Auto-generated method stub
		getLoaderManager().restartLoader(0, null, this);
	}

	@Override
	public void onLoadFinished(Loader<HashMap<ENMapKey, Object>> arg0,
			HashMap<ENMapKey, Object> arg1) {
		// TODO Auto-generated method stub
		ProgressBar progressBar = (ProgressBar)getView().findViewById(R.id.progress_loading);
		if(progressBar!=null)progressBar.setVisibility(View.GONE);
		onLoadTopFragmentSummary(arg1);

	}

	@Override
	public void onLoaderReset(Loader<HashMap<ENMapKey, Object>> arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Loader<HashMap<ENMapKey, Object>> onCreateLoader(int arg0, Bundle arg1) {
		// TODO Auto-generated method stub
		
		ProgressBar progressBar = (ProgressBar)getView().findViewById(R.id.progress_loading);
		if(progressBar!=null){
			progressBar.setVisibility(View.VISIBLE);
		}
        Date dtStart =mDate;
        Date dtEnd = UTILDate.getNextMONTH(dtStart, 1);
        if(getDataPeroidBewteenListener()!=null){
            String periodType = getDataPeroidBewteenListener().ONDataPeriodBewteen(dtStart,getClass());
            if(periodType!=null ){
                if(StringUtils.equalsIgnoreCase(periodType, Recur.DAILY)){
                    dtStart = UTILDate.getCalendarInitSSMMSS(mDate);
                    dtEnd = UTILDate.getNextDAY(dtStart, 1);
                }
            }
        }else{
            dtStart = UTILDate.getInitDateMonthFirstDay(mDate);
            dtEnd = UTILDate.getNextMONTH(dtStart, 1);

        }
        GDB.log(getClass(), "onCreateLoader #### Dtstart\t" + UTILDate.getYYYYMDDHHMMSS(dtStart) +"\tDtEnd=" +UTILDate.getYYYYMDDHHMMSS(dtEnd));
		return new LoaderSQLAccountBetween(getActivity(), dtStart, dtEnd, null);

	}

	@Override
	public void OnDateChange(Date dtStart, Date dtEnd) {
		// TODO Auto-generated method stub
        GDB.log(getClass(), "OnDateChange #### Dtstart\t" + UTILDate.getYYYYMDDHHMMSS(mDate) +"\tDtEnd=" +UTILDate.getYYYYMDDHHMMSS(dtEnd));
		mDate.setTime(dtStart.getTime());
        GDB.log(getClass(), "OnDateChange #### Dtstart\t" + UTILDate.getYYYYMDDHHMMSS(mDate) +"\tDtEnd=" +UTILDate.getYYYYMDDHHMMSS(dtEnd));

        OnReLoad();
	}

	@Override
	public void OnReLoad() {
		// TODO Auto-generated method stub
		if(getLoaderManager()!=null ){
			if(getLoaderManager().getLoader(0)==null){
				getLoaderManager().initLoader(0, null, this);
			}else{
				getLoaderManager().restartLoader(0, null, this);
			}
		}
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		if(getLoaderManager()!=null ){
			if(getLoaderManager().getLoader(0)==null){
				getLoaderManager().initLoader(0, null, this);
			}else{
				getLoaderManager().restartLoader(0, null, this);
			}
		}

		super.onResume();
	}

	public Date getmDate() {
		return mDate;
	}

	public void setmDate(Date mDate) {
		this.mDate = mDate;
	}
	
	public void onLoadTopFragmentSummary(HashMap<ENMapKey,Object> hash){
		FragmentTopSum fragmentTopSum = (FragmentTopSum)getChildFragmentManager().findFragmentById(R.id.fragment_top_sum);
		if(fragmentTopSum!=null){
			fragmentTopSum.bindTextData(hash);
		}

	}
	


}
